package com.bionet.keggservice.service.impl;

import com.bionet.service_base.utils.Result;
import com.bionet.keggservice.entity.KeggFind;
import com.bionet.keggservice.mapper.KeggFindMapper;
import com.bionet.keggservice.service.KeggFindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hzx
 * @since 2021-06-24
 */
@Service
public class KeggFindServiceImpl extends ServiceImpl<KeggFindMapper, KeggFind> implements KeggFindService {
    @Override
    public Result getAllList() {
        List<KeggFind> keggFinds = baseMapper.selectList(null);
        Map<String, Object> options = new HashMap<String,Object>();
        ArrayList<String> labels = new ArrayList<>();
        for (KeggFind entity:keggFinds){
            if(!options.containsKey(entity.getOperation())){
                options.put("value",entity.getOperation());
                options.put("label",entity.getOperation());
                if (entity.getDb() != null){
                    Map<String, Object> Db = new HashMap<String,Object>();
                    if (labels.contains(entity.getDb())){
                        ArrayList childrens = (ArrayList)options.get("children");
                        for (Object children:childrens){
                            Map<String, Object> child = (Map<String, Object>) children;
                            String label = (String)child.get("label");
                            String db = entity.getDb();
                            if (label.equals(db)){
                                Db = child;
                                childrens.remove(Db);
                                break;
                            }
                        }
                    }else {
                        Db.put("value",entity.getDb());
                        Db.put("label",entity.getDb());
                    }
                    if (entity.getArg() != null){
                        Map<String, String> Arg = new HashMap<String,String>();
                        Arg.put("value",entity.getArg());
                        Arg.put("label",entity.getArg());
                        if (!Db.containsKey("children")){
                            Db.put("children",new ArrayList<String>());
                        }
                        ArrayList children = (ArrayList)Db.get("children");
                        children.add(Arg);
                        Db.put("children",children);
                    }
                    if (!options.containsKey("children")){
                        options.put("children",new ArrayList<String>());
                    }
                    ArrayList childrens = (ArrayList)options.get("children");
                    childrens.add(Db);
                    labels.add(entity.getDb());
                    options.put("children",childrens);
                }
            }
        }
        Map[] optionslist = new Map[1];
        optionslist[0] = options;
        return Result.ok().data("optionslist",optionslist);
    }
}
